/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AdminManagerment.jdbc;

import connection.Configuration;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

/**
 *
 * @author Tngoc
 */
public class TotalInterview {

    private Configuration config;
    private Connection conn;
    private int trueInterview;
    private int falseInterview;
    private DefaultPieDataset pieData;
    private DefaultCategoryDataset barData;
    private DefaultCategoryDataset companyData;
    private int month;
    private int year;
    public static int PIECHART = 0;
    public static int BARCHART = 1;

    public TotalInterview(int month, int year, int type) {
        config = new Configuration();
        conn = config.getConnect();
        this.month = month;
        this.year = year;
        getInfor(type);
    }
    
    public TotalInterview(){
        config = new Configuration();
        conn = config.getConnect();
        getComp();
    }

    public DefaultPieDataset getPieData() {
        return pieData;
    }
    
    public DefaultCategoryDataset getBarData() {
        return barData;
    }

    public DefaultCategoryDataset getCompanyData() {
        return companyData;
    }

    public void setCompanyData(DefaultCategoryDataset companyData) {
        this.companyData = companyData;
    }

    public void setData(DefaultPieDataset data) {
        this.pieData = data;
    }

    private void getInfor(int type) {

        try {
            CallableStatement cs = conn.prepareCall("{call countInterview (?,?,?)}");
            cs.setInt(1, 1);
            cs.setInt(2, month);
            cs.setInt(3, year);
            ResultSet rs = cs.executeQuery();
            rs.next();
            this.trueInterview = rs.getInt(1);

            cs.setInt(1, 0);
            cs.setInt(2, month);
            cs.setInt(3, year);
            rs = cs.executeQuery();
            rs.next();
            this.falseInterview = rs.getInt(1);
            if (type == 0) {
                this.pieData = new DefaultPieDataset();
                pieData.setValue("Defeated ", this.falseInterview);
                pieData.setValue("succeed ", this.trueInterview);
            } else {
                this.barData = new DefaultCategoryDataset();
                barData.setValue(this.falseInterview, "unsuccess", "interview");
                barData.setValue(this.trueInterview, "success", "interview");
            }
            rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(TotalInterview.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void getComp(){
        try {
            CallableStatement cs = conn.prepareCall("{call companyInterview}");
            ResultSet rs = cs.executeQuery();
            this.companyData = new DefaultCategoryDataset();
            while(rs.next()){
                companyData.setValue(rs.getInt("total"), "interview", rs.getString("mm"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(TotalInterview.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
